import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr } from '@/utils/BtnControl'
import Query from '@/components/Query';
import { isNumber } from '@/utils/validate';
import { VxeUI } from 'vxe-pc-ui';
import XEUtils from 'xe-utils';
import XLSX from 'xlsx';
export default {
  name: 'MessageType',
  components: {
    Query
  },
  data() {
    return {
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      btnPromission: '',
      editModal: false,
      BaseApiUrl: '/api/Tpmmessagetype',
      importModal: false,
      QueryConditions: [],
      QueryConfig: [{
        title: '消息类型',
        field: 'xxlx',
        type: 'input',
        value: '',
        placeholder: '请输入消息类型以查询',
      }
      ],
      GridOptions: {
        id: 'MessageType_MainGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 700,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent: true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          custom: true,
          export: true,
          slots: {
            buttons: 'customButtons'
          }
        },
        customConfig: {
          mode: 'drawer',
          storage: true
        },
        columns: [
          { type: 'seq', width: 50 },
          { type: 'checkbox', width: 35 },
          { field: 'xxlx', title: '消息类型', sortable: true, minWidth: 100, showOverflow: true },
          { field: 'xxms', title: '消息描述', sortable: true, minWidth: 300, showOverflow: true },  
          { field: 'lcjd', title: '流程节点', sortable: true, width: 100, showOverflow: true ,slots:{default:'lcjdStatus'}}, 
          { field: 'txzq', title: '提醒周期(天)', sortable: true, width: 120, showOverflow: true },         
          { field: 'createtime', title: '创建时间', width: 150, showOverflow: true },
          { field: 'createname', title: '创建人', width: 100, showOverflow: true },
          { field: 'updatetime', title: '更新时间', width: 150, showOverflow: true },
          { field: 'updatename', title: '更新人', width: 100, showOverflow: true },
          { field: 'operation', title: '操作', fixed: 'right', width: 120, slots: { default: 'operation' } },
        ],
        data: []
      },
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      FormOptions: {
        'title-width': 100,
        border: true,
        titleBackground: true,
        data: {
          id: -1,
          createid: '',
          createtime: '',
          createname: '',
          updateid: '',
          updatetime: '',
          updatename: '',
          xxlx: '',
          xxms: '',
          lcjd: false,
          txzq: undefined,
        },
        rules: {
          xxlx: [{ required: true, message: '请输入消息类型', trigger: 'blur' }]
        },
        items: [
          { field: 'xxlx', title: '消息类型', span: 24, itemRender: { name: 'VxeInput', placeholder: '请输入客户编码'} },
          { field: 'xxms', title: '消息描述', span: 24, 
            itemRender: { name: 'VxeTextarea', maxlegnth: 200, clearable: true, showWordCount: true , placeholder: '请输入消息描述'}},
          { field: 'lcjd', title: '流程节点', span: 24, 
            itemRender: { name: 'VxeSwitch',props:{'open-value':'Y','close-value':'N','open-label':'是','close-label':'否'} }},
          { field: 'txzq', title: '提醒周期(天)', span: 24, 
            itemRender: { name: 'VxeNumberInput',props:{type:'integer'}, placeholder: '请输入提醒周期(天)'} },
          {
            align: 'center',
            span: 24,
            itemRender: {
              name: 'VxeButtonGroup',
              options: [{
                type: 'submit', content: '提交', status: 'primary'
              }, {
                type: 'reset', content: '重置'
              }]
            }
          }
        ]
      },
    }
  },
  created() {
    // 动态头部按钮
    this.btnPromission = getBtnCtr(
      this.$route.path,
      this.$store.getters.userPermission
    )
  },
  methods: {
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoQuery(e);
    },
    handlePageChange() {
      this.DoQuery(this.QueryConditions);
    },
    handleBatchDelete() {
      let rows = this.$refs.maingrid.getCheckboxRecords(true);
      this.DoBatchDelete(rows);
    },
    handleAdd() {
      this.FormOptions.data = {
        id: -1,
        createid: '',
        createtime: '',
        createname: '',
        updateid: '',
        updatetime: '',
        updatename: '',
        xxlx: '',
        xxms: '',
        lcjd: false,
        txzq: undefined,
      }
      this.editModal = true;
    },
    handleEdit(row) {
      this.FormOptions.data = row;
      this.editModal = true;
    },
    handleDelete(row) {
      this.DoDelete(row);
    },
    handleEditModalSubmit() {
      this.FormOptions.data.id == -1 ? this.DoAdd() : this.DoEdit();
    },
    handleEditModalReset() {

    },
    DoQuery(e) {
      let request = {
        isPaging: true,
        pageSize: this.PagerConfig.pageSize,
        pageIndex: this.PagerConfig.currentPage,
        queryConditions: e,
        orderByConditions: []
      }
      CURDHelper.Query(request, this.BaseApiUrl)
        .then(res => {
          this.GridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;
        }).catch(err => {
          VxeUI.modal.message({
            content: err,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoAdd() {
      //新增
      CURDHelper.Add(this.FormOptions.data, null, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '新增数据成功',
            status: 'success',
            duration: this.duration
          })
          this.editModal = false;
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({
            content: err,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoEdit() {
      //编辑
      CURDHelper.EditWithConfirm('是否修改当前数据', this.FormOptions.data, null, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '编辑数据成功',
            status: 'success',
            duration: this.duration
          });
          this.DoQuery(this.QueryConditions);
          this.editModal = false;
        }).catch(err => {
          VxeUI.modal.message({
            content: err,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoDelete(row) {
      //删除
      let deleteModel = {
        ID: row.id,
        DeleteRemark: ''
      };
      CURDHelper.DeleteWithConfirm('是否删除当前数据', deleteModel, null, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '删除数据成功',
            status: 'success',
            duration: this.duration
          })
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({
            content: err,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoBatchDelete(rows) {
      //删除
      let deleteList = [];
      rows.forEach(row => {
        deleteList.push({
          ID: row.id,
          DeleteRemark: ''
        })
      })
      CURDHelper.DeleteWithConfirm('是否删除当前已选择数据，删除后不能恢复', null, deleteList, this.BaseApiUrl)
        .then(res => {
          VxeUI.modal.message({
            content: '删除数据成功',
            status: 'success',
            duration: this.duration
          })
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({
            content: err,
            status: 'error',
            duration: this.duration
          })
        })
    },
  }
}
